﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
using EAF.Cache;
using EAF.Utility;
using EAF.Server;
using EAF.DBC;
using App.Model;
using EAF.DBC.Model;
using App.IServer.Log;
using App.Utility.ComEntity;
using App.Utility;

namespace App.Server.Log
{
    /// <summary>
    /// NormalLog表相关操作
    /// </summary>
    public class LogNormalService : DBCBaseService<LogNormal, AppLogContext>, ILogNormalService
    {
        //通过core注入 context
        public LogNormalService(AppLogContext context) : base(context as AppLogContext)
        {

        }





        #region 增删改查列表分页等基础操作

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>插入记录id</returns>
        public int Add(LogNormal entity)
        {
            entity.Deleted = false;
            entity.CreateTime = DateTime.Now;
            return DBC.Insert(entity);
        }

        /// <summary>
        /// 更新 支持自定义表名
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="tableName">表名</param>
        /// <returns>更新成功数量</returns>
        public int Update(LogNormal entity, string tableName)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                var lstTable = DBC.TableList("LogNormal_").ToList();
                if (lstTable.Count > 0)
                {
                    tableName = lstTable[lstTable.Count - 1];
                }
            }
            return DBC.Update(entity, ( old) => tableName);
        }

        /// <summary>
        /// 删除 支持自定义表名
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="tableName">表名</param>
        /// <param name="mode">删除模式0假删除 1真删除</param>
        /// <returns>影响行数</returns>
        public int Delete(int id, string tableName, int mode = 0)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                var lstTable = DBC.TableList("LogNormal_").ToList();
                if (lstTable.Count > 0)
                {
                    tableName = lstTable[lstTable.Count - 1];
                }
            }
            if (mode == 0)
            {
                var entity = DBC.QueryFirst<LogNormal>(id, (type, old) => tableName);
                if (entity != null)
                {
                    entity.DeleteTime = DateTime.Now;
                    entity.Deleted = true;
                }
                return DBC.Update(entity, (old) => tableName);
            }
            else
            {
                return DBC.Delete<LogNormal>(id, (old) => tableName);
            }

        }

        /// <summary>
        /// 删除 使用拉姆达 支持自定义表名
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="tableName">表名</param>
        /// <param name="mode">删除模式0假删除 1真删除</param>
        /// <returns>影响行数</returns>
        public int Delete(Expression<Func<LogNormal, bool>> where, string tableName, int mode = 0)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                var lstTable = DBC.TableList("LogNormal_").ToList();
                if (lstTable.Count > 0)
                {
                    tableName = lstTable[lstTable.Count - 1];
                }
            }
            if (mode == 0)
            {
                var entity = DBC.QueryFirst<LogNormal>(where, true, (type, old) => tableName);
                if (entity != null)
                {
                    entity.DeleteTime = DateTime.Now;
                    entity.Deleted = true;
                }
                return DBC.Update(entity, (old) => tableName);
            }
            else
            {
                return DBC.Delete(where, (old) => tableName);
            }

        }

        /// <summary>
        /// 获取单个 支持自定义表名
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>数据实体</returns>
        public LogNormal Detail(int id, string tableName = null)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                var lstTable = DBC.TableList("LogNormal_").ToList();
                if (lstTable.Count > 0)
                {
                    tableName = lstTable[lstTable.Count - 1];
                }
            }
            return DBC.QueryFirst<LogNormal>(id, (type, old) => tableName);
        }

        /// <summary>
        /// 获取列表 使用拉姆达
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="showAll">显示所有</param>
        /// <param name="orderBy">排序</param>
        /// <param name="fields">select字段</param>
        /// <returns>数据集合</returns>
        public List<LogNormal> List8Lambda(Expression<Func<LogNormal, bool>> where, bool showAll = false, Expression<Func<IQueryable<LogNormal>, IOrderedQueryable<LogNormal>>> orderBy = null,
            Expression<Func<LogNormal, object>> fields = null)
        {
            if (!showAll)
            {
                if (where == null)
                {
                    where = a => a.Deleted == false;
                }
                else
                {
                    where = where.And(a => a.Deleted == false);
                }
            }

            return DBC.Query8Lambda(where, orderBy, fields);
        }

        /// <summary>
        /// 获取分页列表 使用拉姆达 
        /// </summary> 
        /// <param name="where">条件</param>
        /// <param name="where4Front">条件 如a='1' and b='2'</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页数量</param>
        /// <param name="totalCount">总记录数</param> 
        /// <param name="paramMode">参数模式 0整除处理null 1不处理null</param>
        /// <param name="showAll">显示所有</param>
        /// <param name="orderBy">排序表达式 如o => o.OrderByDescending(c => c.CreateTime)</param>
        /// <param name="orderBy4Front">排序表达式 前端table适配  如order by a desc</param>
        /// <param name="fields">select字段</param>
        /// <returns>数据集合</returns>
        public List<LogNormal> PageList8Lambda(Expression<Func<LogNormal, bool>> where, string where4Front, int pageIndex, int pageSize, ref int totalCount, int paramMode = 1, bool showAll = false,
            Expression<Func<IQueryable<LogNormal>, IOrderedQueryable<LogNormal>>> orderBy = null, string orderBy4Front = null, string tableName = null, Expression<Func<LogNormal, object>> fields = null)
        {
            if (!showAll)
            {
                if (where == null)
                {
                    where = a => a.Deleted == false;
                }
                else
                {
                    where = where.And(a => a.Deleted == false);
                }

                if (!string.IsNullOrWhiteSpace(where4Front))
                {
                    where4Front += " AND Deleted=0";
                }
            }
            if (string.IsNullOrEmpty(tableName))
            {
                var lstTable = DBC.TableList("LogNormal_").ToList();
                if (lstTable.Count == 0)
                {
                    totalCount = 0;
                    return new List<LogNormal>();
                }
                tableName = lstTable[lstTable.Count - 1];
            }
            return DBC.QueryPage8Lambda(where, where4Front, pageIndex, pageSize, ref totalCount, paramMode, orderBy, orderBy4Front, fields, tableName);
        }


        #endregion

    }
}


